package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.U_cate;

import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.U_cateMapper;

import cn.com.api.service.U_cateService;
import cn.com.api.service.impl.base.U_cateServiceImplBase;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class U_cateServiceImpl extends U_cateServiceImplBase implements U_cateService {
	@Autowired
	private U_cateMapper U_cateMapper;
	@Autowired
	private ApiU_favoriteMapper apiU_favoriteMapper;

	@Override
	public List<U_cate> findU_catesOrderByParentId(Integer parent_id) {
		// TODO
		List<U_cate> u_cates = U_cateMapper.findU_catesOrderByParentId(parent_id);
		return u_cates;
	}

	// 获取头条分类
	@Override
	public List<U_cate> getArticleCates(Integer parentid, Integer userid, String cityReplace) {

		List<U_cate> cate_list = U_cateMapper.getArticleCates(parentid, userid);
		// 将Location分类替换成cityReplace
		for (U_cate cate : cate_list) {
			if (cate.getCate_type() == 1) {
				cate.setCate_name(cityReplace);
			}
		}

		return cate_list;
	}

	// 更新用户关注分类
	@Override
	public void updateUserArticleCates(String cate_ids, String user_id) {
		// 删除原有头条关注
		Map<String, String> params = new HashMap<String, String>();
		params.put("user_id", user_id);
		params.put("type", "3");
		this.apiU_favoriteMapper.deleteU_favoriteByMap(params);
		// Insert new info
		String cates[] = cate_ids.split(":");
		for (String cate_id : cates) {
			U_cate u_cate = this.U_cateMapper.findU_cateById(Integer.valueOf(cate_id));
			ApiU_favorite favorite = new ApiU_favorite();
			favorite.setAdd_time(Util.CurrentTime());
			favorite.setConn_id(u_cate.getId());
			favorite.setType(3);
			favorite.setUser_id(Integer.valueOf(user_id));
			this.apiU_favoriteMapper.insertU_favorite(favorite);
		}
	}

}
